﻿using RSALicUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RSALicManager
{
    public partial class AuthToolsForm : Form
    {
        public AuthToolsForm()
        {
            InitializeComponent();
        }

        private void QrParser_KeyDetected(string obj)
        {
            if (!string.IsNullOrEmpty(obj))
            {
                tbLicData.Text = obj;
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            tbPubKey.Text = "<RSAKeyValue><Modulus>s07fVBQGgHwtcnlcEdRyVE0BBiNPeQULo9+N3UXXRzIU0yJNZoiajRI5hzSaTcbtAD6cJVDqDbJQDZif4zfpP0WrYoIJPkVHg1W7s+q1Ji7vGZ4EN3391pjF9ZapUbMY4xemRgCakZveemacNY0nQpN4zuEVamZF47+CWylK9vU=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

            if (!AuthForm.VertifyLic())
            {
                // Close();
            }
        }

        string priKey = "";

        private void MenuMakeKeys_Click(object sender, EventArgs e)
        {
            var keys = RSALicCore.KeyMaker.Make();
            tbPubKey.Text = keys[0];
            priKey = keys[1];
        }

        private void MenuGetPriKey_Click(object sender, EventArgs e)
        {
            MessageBox.Show(priKey);
        }

        string hardwareHash;
        private void MenuGetLicData_Click(object sender, EventArgs e)
        {
            hardwareHash = RSALicCore.HardwareInfoReader.Read();            
            var pubmaker = new RSALicCore.PubLicManager(tbPubKey.Text);
            tbLicData.Text = pubmaker.MakeLic(hardwareHash);
        }


        private void MenuVertifyLic_Click(object sender, EventArgs e)
        {
            var pubmaker = new RSALicCore.PubLicManager(tbPubKey.Text);
            var res = pubmaker.VertifyLic(tbLic.Text, hardwareHash);
            var msg = res ? "验证通过" : "授权错误";
            MessageBox.Show(msg);
        }


        private void MenuLoadQr_Click(object sender, EventArgs e)
        {
            using (var dlg = new QRCodeParseForm())
            {
                var res = dlg.ShowDialog();
                if (res == DialogResult.OK)
                {
                    var licdata = dlg.GetQrResult();
                    tbLicData.Text = licdata;
                }
            }
        }

        private void MenuMakeTestQr_Click(object sender, EventArgs e)
        {
            var msg = CommResources.TestQrData;
            using (var bmp = RSALicCore.QRCodeHelper.Create(msg))
            using (var dlg = new SaveFileDialog())
            {
                dlg.Filter = "图片文件|*.png";
                dlg.Title = "保存二维码图片";
                var res = dlg.ShowDialog();
                if (res == DialogResult.OK)
                {
                    bmp.Save(dlg.FileName);
                }
            }
        }

        private void MenuTestQrParse_Click(object sender, EventArgs e)
        {
            using (var dlg = new QRCodeParseProForm())
            {
                var res = dlg.ShowDialog();
                if (res == DialogResult.OK)
                {
                    var licdata = dlg.GetQrResult();
                    tbLicData.Text = licdata;
                }
            }
        }
    }
}
